package com.yanqu.road.server.manager.activity.operamask;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.operamask.OperaMaskSceneRank;
import com.yanqu.road.logic.activity.operamask.OperaMaskSceneRankListModel;
import com.yanqu.road.logic.bussiness.activity.OperaMaskBusiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manager.activity.ActivityInfoMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossOperaMaskMgr extends TempMgr {

    /**
     * 剧目榜    活动ID，类型，剧目ID，榜单
     */
    private static final Map<Integer, Map<Integer, Map<Integer, OperaMaskSceneRankListModel>>> activityTypeSceneModelMap = new ConcurrentHashMap<>();

    /**
     * 更新榜单
     */
    public static void rankChange(OperaMaskSceneRank userRank) {
        OperaMaskSceneRankListModel listModel = getOperaMaskSceneRankListModel(userRank.getActivityId(), userRank.getType(), userRank.getSceneId());
        listModel.rankChange(userRank);
    }

    /**
     * 获取榜单
     */
    public static OperaMaskSceneRankListModel getOperaMaskSceneRankListModel(int activityId, int type, int sceneId) {
        if (activityTypeSceneModelMap.containsKey(activityId)
                && activityTypeSceneModelMap.get(activityId).containsKey(type)
                && activityTypeSceneModelMap.get(activityId).get(type).containsKey(sceneId)) {
            return activityTypeSceneModelMap.get(activityId).get(type).get(sceneId);
        }
        synchronized (activityTypeSceneModelMap) {
            if (!activityTypeSceneModelMap.containsKey(activityId)) {
                Map<Integer, Map<Integer, LinkedList<OperaMaskSceneRank>>> typeSceneListMap = OperaMaskBusiness.getOperaMaskSceneRankMap(activityId);
                Map<Integer, Map<Integer, OperaMaskSceneRankListModel>> tmpTypeSceneMap = new ConcurrentHashMap<>();
                for (Map.Entry<Integer, Map<Integer, LinkedList<OperaMaskSceneRank>>> entry : typeSceneListMap.entrySet()) {
                    int tmpType = entry.getKey();
                    Map<Integer, LinkedList<OperaMaskSceneRank>> tmpSceneListMap = entry.getValue();
                    for (Map.Entry<Integer, LinkedList<OperaMaskSceneRank>> entry2 : tmpSceneListMap.entrySet()) {
                        int tmpSceneId = entry2.getKey();
                        LinkedList<OperaMaskSceneRank> tmpList = entry2.getValue();
                        tmpTypeSceneMap.putIfAbsent(tmpType, new ConcurrentHashMap<>());
                        tmpTypeSceneMap.get(tmpType).put(tmpSceneId, new OperaMaskSceneRankListModel(activityId, tmpType, tmpSceneId, 100, tmpList));
                    }
                }
                activityTypeSceneModelMap.put(activityId, tmpTypeSceneMap);
            }
            Map<Integer, Map<Integer, OperaMaskSceneRankListModel>> typeSceneModelMap = activityTypeSceneModelMap.get(activityId);
            if (!typeSceneModelMap.containsKey(type)) {
                typeSceneModelMap.put(type, new ConcurrentHashMap<>());
            }
            Map<Integer, OperaMaskSceneRankListModel> sceneModelMap = typeSceneModelMap.get(type);
            if (!sceneModelMap.containsKey(sceneId)) {
                sceneModelMap.put(sceneId, new OperaMaskSceneRankListModel(activityId, type, sceneId, 100, new LinkedList<>()));
            }
            return sceneModelMap.get(sceneId);
        }
    }

    @Override
    public boolean save() {
        for (Map<Integer, Map<Integer, OperaMaskSceneRankListModel>> mapMap : activityTypeSceneModelMap.values()) {
            for (Map<Integer, OperaMaskSceneRankListModel> listModelMap : mapMap.values()) {
                for (OperaMaskSceneRankListModel listModel : listModelMap.values()) {
                    listModel.save();
                }
            }
        }

        // 移除旧数据
        long now = System.currentTimeMillis() / 1000;
        for (Integer activityId : activityTypeSceneModelMap.keySet()) {
            ActivityInfo activityInfo = ActivityInfoMgr.getActivityInfo(activityId);
            if (activityInfo == null || now > activityInfo.getEndShowTime() + DateHelper.HOUR_SECONDS * 12) { // 过期12小时候移除
                activityTypeSceneModelMap.remove(activityId);
            }
        }

        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }
}
